import { Component, OnInit } from "@angular/core";
import { NzNotificationService } from "ng-zorro-antd";
import { Router, ActivatedRoute } from "@angular/router";
import { HttpServcie } from "../../../service/http.service";
import { Config } from "../../../service/config";
import { InspectionRecord } from "./bean/inspection-record.bean";
import { el } from "@angular/platform-browser/testing/src/browser_util";
import { NzModalService, NzModalSubject } from "ng-zorro-antd";

@Component({
  selector: "inspection-record",
  templateUrl: "inspection-record.html",
  styleUrls: ["inspection-record.scss"]
})
export class InspectionPage implements OnInit {
  data: any;
  list: any = []; //返回的数据
  //接收参数
  inspectionBean: InspectionRecord = new InspectionRecord();
  //总页数
  total: string;
  //多选框
  allChecked: any = false;
  config = Config;
  picList = [];
  //多选框
  indeterminate: any = false;
  inValue: any = {};
  partyType = JSON.parse(window.localStorage["user"]).partyType;
  searchOptions = [
    {
      name: "正常",
      value: "0"
    },
    {
      name: "异常",
      value: "1"
    }
  ];
  elevatorType: string = "";
  constructor(
    private httpServcie: HttpServcie,
    private router: Router,
    private _notification: NzNotificationService,
    private nsub: NzModalSubject,
    private modalService: NzModalService,
    private activatedRoute: ActivatedRoute,
    private currentModal: NzModalSubject
  ) {
    this.activatedRoute.params.subscribe(data => {
      //深度 clone 一下，否则不能修改
      this.inValue = Object.assign({}, data);
    });
    this.inspectionBean.pageSize = 10;
    this.inspectionBean.currentPage = 1;
    this.inspectionBean.elevatorId = this.inValue.elevatorId;
    this.elevatorType = this.inValue.elevatorType;
    this.total = "0";
  }

  ngOnInit() {
    this.query();
  }

  //table加载数据时
  displayDataChange($event) {
    this.list = $event;
    this.refreshStatus();
  }

  //重置选择框
  refreshStatus() { }
  //全选
  checkAll(value) {
    if (value) {
      this.list.forEach(data => {
        if (!data.disabled) {
          data.checked = true;
        }
      });
    } else {
      this.list.forEach(data => (data.checked = false));
    }
  }

  //分页
  nzPageSizeChange(event) {
    this.inspectionBean.pageSize = event;
    this.inspectionBean.currentPage = 1;
    this.query();
  }

  //输入框的方法
  nzPageIndexChange(event) {
    this.inspectionBean.currentPage = event;
    this.query();
  }

  //当前页
  nzPageIndexClickChange(event) {
    this.inspectionBean.currentPage = event;
    this.query();
  }

  //点击查看跳转对应的详情页
  toDetail(data) {
    if (this.elevatorType == "4" || this.elevatorType == "6") {
      this.router.navigate(["/home/inspection-ftrecord", { id: data.id }]);
    } else {
      this.router.navigate(["/home/inspection-detail", { id: data.id }]);
    }
  }

  toBack() {
    window.history.back();
  }

  query() {
    //查询当前电梯的评价
    this.httpServcie.getInspectionList(this.inspectionBean).subscribe(
      data => {
        if (data.status == 200) {
          this.list = data.data.list;
          this.total = data.data.total;
        } else if (data.status === 401) {
          this.router.navigateByUrl("/login");
          return;
        } else {
          // 异常
          this._notification.create("error", "错误提示", data.message);
        }
      },
      error => {
        console.log(JSON.stringify(error));
      }
    );
  }

  showLargeImage(titlepic, contentpic, footerpic, img) {
    this.picList = [];
    this.picList.push(img);
    this.currentModal = this.modalService.open({
      title: titlepic,
      content: contentpic,
      footer: footerpic,
      maskClosable: false,
      width: 800,
      style: {
        top: "20px"
      },
      onOk() {
        console.log("Click ok");
      }
    });
  }

  exportThisMonth() {
    let nowdate = new Date();
    let mon = (nowdate.getMonth() + 1) + "";
    if (((nowdate.getMonth() + 1) + "").length == 1) {
      mon = "0" + (nowdate.getMonth() + 1);
    }
    let nowmont = nowdate.getFullYear() + "" + mon;

    let param = {
      elevatorId: this.inValue.elevatorId,
      month: nowmont,
      elevatorType: this.elevatorType
    };

    this.httpServcie.exportDaily(param).subscribe(data => {
      if (data.status == 200) {
        this.download(data.data.fileUrl, data.data.fileName);
        this._notification.create("success", "提示", "导出成功");
      } else {
        this._notification.create("error", "提示", "导出失败");
      }
    });
  }

  download(url, name) {
    let link = document.createElement("a");
    let body = document.querySelector("body");

    this.getBlob(this.config.IP + this.config.API.getUpload + url).then(
      blob => {
        link.href = window.URL.createObjectURL(blob);
        link.download = name;
        // fix Firefox
        link.style.display = "none";
        body.appendChild(link);
        link.click();
        body.removeChild(link);
        window.URL.revokeObjectURL(link.href);
      }
    );
  }
  getBlob(url) {
    return new Promise(resolve => {
      const xhr = new XMLHttpRequest();
      xhr.open("GET", url, true);
      xhr.responseType = "blob";
      xhr.onload = () => {
        if (xhr.status === 200) {
          resolve(xhr.response);
        }
      };
      xhr.send();
    });
  }
}
